<DOCTYPE html>
  <html lang="en">

  <head charset="utf-8">
    <title></title>
  </head>

  <body>
    <script>
      function Queue() {
        this.array = [];

        Queue.prototype.enqueue = element => {
          this.array.push(element);
        }

        Queue.prototype.dequeue = () => {
          return this.array.shift();
        }

        Queue.prototype.front = () => {
          return this.array[0];
        }

        Queue.prototype.isEmpty = () => {
          return this.array.length == 0
        }

        Queue.prototype.size = () => {
          return this.array.length
        }

        Queue.prototype.toString = () => {
          let result = ''
          for (let i = 0; i < this.size(); i++) {
            result += this.array[i] + ' '
          }
          return result
        }

        //返回数组的函数
        Queue.prototype.listShow = () => {
          return this.array
        }
      }


      //小游戏击鼓传花
      //要跟上面构造函数一起用
      function passGame(arr, num) {
        if (arr.isEmpty()) return false
        let index = 0
        while (arr.size() > 1) {
          index++;
          let element = arr.dequeue();
          arr.enqueue(element);
          if (index == num) {
            arr.dequeue();
            index = 0;
          }
        }
        return arr.front()
      }

      //优先级队列
      function PriorityQueue() {
        this.array = [];

        PriorityQueue.prototype.queueElement = (element, priority) => {
          this.element = element;
          this.priority = priority;
        }

        PriorityQueue.prototype.enqueue = (element, priority) => {
          let item = this.queueElement(element, priorty);
          if (this.size() == 0) {
            this.array.push(item)
          }
          for (let i = this.size(); i > 0; i--) {
            if (item.priorty > this.array[i].priority) {
              this.array.splice(i, 0, item)
              return
            }
          }
        }

        PriorityQueue.prototype.dequeue = () => {
          return this.array.shift();
        }

        PriorityQueue.prototype.front = () => {
          return this.array[0];
        }

        PriorityQueue.prototype.isEmpty = () => {
          return this.array.length == 0
        }

        PriorityQueue.prototype.size = () => {
          return this.array.length
        }

        PriorityQueue.prototype.toString = () => {
          let result = ''
          for (let i = 0; i < this.size(); i++) {
            result += this.array[i].element + '-' + this.array[i].priority + '<br/>'
          }
          return result
        }
      }
    </script>
  </body>

  </html>